package com.gwd.task.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gwd.task.entity.CodeRepositoryEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhiyongming
 * @date 2020/12/22 14:20
 * 代码仓库表
 */
@Data
@NoArgsConstructor
public class CodeRepositoryDTO {
    private Long crId;
    private String address;
    @NotBlank(message = "类型不能为空")
    private String type;
    private String lastChangeTime;
    private Long lastChangeUser;
    private String lastChangeUserName;
    private Integer status;
    private String members;
    @NotNull(message = "项目组不能为空")
    private Long[] projectIds;
    private Long projectId;
    private Integer num; //数量
    private List<Long> membersList;


    public CodeRepositoryDTO(CodeRepositoryEntity entity) {
        BeanUtil.copyProperties(entity, this, CopyOptions.create().setIgnoreError(false).setIgnoreNullValue(true));

        if (entity.getMembers()!=null && !entity.getMembers().equals("")) {
            String[] s = entity.getMembers().split(",");
            this.membersList = new ArrayList<>();
            for (String s1 : Arrays.stream(s).collect(Collectors.toList())) {
                this.membersList.add(Long.valueOf(s1));
            }
        }
    }

    public void matchNumNull() {
        this.num = this.num==null?0:this.num;
    }
}
